<?php


namespace app\admin\controller;

use app\api\controller\UserApi;

class User extends BaseController
{
   
    public function lists()
    {
        // 返回模板视图
        return $this->fetch();
    }

   
    public function userList()
    {
      
        $UserApi = new UserApi();

     
        $list = $UserApi->getList();

      
        foreach ($list as &$v) {
            if ($v['sex'] == 1) {
                $v['sex'] = '男';
            } else if ($v['sex'] == 2) {
                $v['sex'] = '女';
            }
        }

    
        return json($list);
    }


   
    public function switchStatus()
    {
        $UserApi = new UserApi();
        $data["id"] = trim(input('post.id'));
        $data["status"] = trim(input('post.status'));
        $response = $UserApi->switchStatus($data);

       
        return $response;
    }

   
    public function add()
    {
        // 返回模板视图
        return $this->fetch();
    }

    public function addUser()
    {
        
        $UserApi = new UserApi();

        $user["username"] = trim(input('post.username'));
        $user["truename"] = trim(input('post.truename'));
        $user["password"] = sha256(trim(input('post.password')));
        $user["birth"] = trim(input('post.birth'));
        $user["sex"] = trim(input('post.sex'));
        $user["email"] = trim(input('post.email'));
        $user["phone"] = trim(input('post.phone'));
        $user["status"] = trim(input('post.status'));

     
        $file = request()->file('avatar');

     
        if ($file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload');
            if ($info) {
        
                $user['avatar'] = "/upload/".$info->getSaveName();
            } else {
            
                $this->error($info->getError());
            }
        }

      
        $response = $UserApi->addUser($user);

       
        return $response;

    }

}